home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gchess40.lha / gnuchess4.0p62 / src / init.c < prev    next >
C/C++ Source or Header  |  1993-06-24  |  13KB  |  492 lines

  1. /*
  2.  * init.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23. #include "gnuchess.h"
  24. #ifdef HASGETTIMEOFDAY
  25. #include <sys/time.h>
  26. #endif
  27. extern unsigned int TTadd;
  28. unsigned int ttbllimit;
  29.  
  30. /* .... MOVE GENERATION VARIABLES AND INITIALIZATIONS .... */
  31.  
  32.  
  33. short distdata[64][64], taxidata[64][64];
  34.  
  35. #ifdef KILLT
  36. /* put moves to the center first */
  37. void
  38. Initialize_killt (void)
  39. {
  40.   register unsigned short f, t, s;
  41.   register short d;
  42.   for (f = 64; f--;)
  43.     for (t = 64; t--;)
  44.       {
  45.     d = taxidata[f][0x1b];
  46.     if (taxidata[f][0x1c] < d)
  47.       d = taxidata[f][0x1c];
  48.     if (taxidata[f][0x23] < d)
  49.       d = taxidata[f][0x23];
  50.     if (taxidata[f][0x24] < d)
  51.       d = taxidata[f][0x24];
  52.     s = d;
  53.     d = taxidata[t][0x1b];
  54.     if (taxidata[t][0x1c] < d)
  55.       d = taxidata[t][0x1c];
  56.     if (taxidata[t][0x23] < d)
  57.       d = taxidata[t][0x23];
  58.     if (taxidata[t][0x24] < d)
  59.       d = taxidata[t][0x24];
  60.     s -= d;
  61.     killt[(f << 8) | t] = s;
  62.     killt[(f << 8) | t | 0x80] = s;
  63.       }
  64. }
  65. #endif
  66. void
  67. Initialize_dist (void)
  68. {
  69.   register short a, b, d, di;
  70.  
  71.   for (a = 0; a < 64; a++)
  72.     for (b = 0; b < 64; b++)
  73.       {
  74.     d = abs (column (a) - column (b));
  75.     di = abs (row (a) - row (b));
  76.     taxidata[a][b] = d + di;
  77.     distdata[a][b] = (d > di ? d : di);
  78.       }
  79. #ifdef KILLT
  80.   Initialize_killt ();
  81. #endif
  82. }
  83.  
  84. const short Stboard[64] =
  85. {rook, knight, bishop, queen, king, bishop, knight, rook,
  86.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  87.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  88.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  89.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  90.  rook, knight, bishop, queen, king, bishop, knight, rook};
  91. const short Stcolor[64] =
  92. {white, white, white, white, white, white, white, white,
  93.  white, white, white, white, white, white, white, white,
  94.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  95.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  96.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  97.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  98.  black, black, black, black, black, black, black, black,
  99.  black, black, black, black, black, black, black, black};
  100. short board[64], color[64];
  101.  
  102. /* given epsquare, from where can a pawn be taken? */
  103. const short epmove1[64] =
  104. {0, 1, 2, 3, 4, 5, 6, 7,
  105.  8, 9, 10, 11, 12, 13, 14, 15,
  106.  16, 24, 25, 26, 27, 28, 29, 30,
  107.  24, 25, 26, 27, 28, 29, 30, 31,
  108.  32, 33, 34, 35, 36, 37, 38, 39,
  109.  40, 32, 33, 34, 35, 36, 37, 38,
  110.  48, 49, 50, 51, 52, 53, 54, 55,
  111.  56, 57, 58, 59, 60, 61, 62, 63};
  112. const short epmove2[64] =
  113. {0, 1, 2, 3, 4, 5, 6, 7,
  114.  8, 9, 10, 11, 12, 13, 14, 15,
  115.  25, 26, 27, 28, 29, 30, 31, 23,
  116.  24, 25, 26, 27, 28, 29, 30, 31,
  117.  32, 33, 34, 35, 36, 37, 38, 39,
  118.  33, 34, 35, 36, 37, 38, 39, 47,
  119.  48, 49, 50, 51, 52, 53, 54, 55,
  120.  56, 57, 58, 59, 60, 61, 62, 63};
  121.  
  122.  
  123. /*
  124.  * nextpos[piece][from-square] , nextdir[piece][from-square] gives vector of
  125.  * positions reachable from from-square in ppos with piece such that the
  126.  * sequence    ppos = nextpos[piece][from-square]; pdir =
  127.  * nextdir[piece][from-square]; u = ppos[sq]; do { u = ppos[u]; if(color[u]
  128.  * != neutral) u = pdir[u]; } while (sq != u); will generate the sequence of
  129.  * all squares reachable from sq.
  130.  *
  131.  * If the path is blocked u = pdir[sq] will generate the continuation of the
  132.  * sequence in other directions.
  133.  */
  134.  
  135. unsigned char nextpos[8][64][64];
  136. unsigned char nextdir[8][64][64];
  137.  
  138. /*
  139.  * ptype is used to separate white and black pawns, like this; ptyp =
  140.  * ptype[side][piece] piece can be used directly in nextpos/nextdir when
  141.  * generating moves for pieces that are not black pawns.
  142.  */
  143. const short ptype[2][8] =
  144. { { no_piece, pawn, knight, bishop, rook, queen, king, no_piece },
  145.   { no_piece, bpawn, knight, bishop, rook, queen, king, no_piece } };
  146.  
  147. /* data used to generate nextpos/nextdir */
  148. static const short direc[8][8] =
  149. {
  150.    { 0, 0, 0, 0, 0, 0, 0, 0 },
  151.    { 10, 9, 11, 0, 0, 0, 0, 0 },
  152.    { 8, -8, 12, -12, 19, -19, 21, -21 },
  153.    { 9, 11, -9, -11, 0, 0, 0, 0 },
  154.    { 1, 10, -1, -10, 0, 0, 0, 0 },
  155.    { 1, 10, -1, -10, 9, 11, -9, -11 },
  156.    { 1, 10, -1, -10, 9, 11, -9, -11 },
  157.    { -10, -9, -11, 0, 0, 0, 0, 0 } };
  158. static const short max_steps[8] =
  159. {0, 2, 1, 7, 7, 7, 1, 2};
  160. static const short nunmap[120] =
  161. {
  162.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  163.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  164.   -1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
  165.   -1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
  166.   -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
  167.   -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
  168.   -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
  169.   -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
  170.   -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
  171.   -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
  172.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  173.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  174.  
  175. int InitFlag = false;
  176. void
  177. Initialize_moves (void)
  178.  
  179. /*
  180.  * This procedure pre-calculates all moves for every piece from every square.
  181.  * This data is stored in nextpos/nextdir and used later in the move
  182.  * generation routines.
  183.  */
  184.  
  185. {
  186.   short ptyp, po, p0, d, di, s, delta;
  187.   unsigned char *ppos, *pdir;
  188.   short dest[8][8];
  189.   short steps[8];
  190.   short sorted[8];
  191.  
  192.   for (ptyp = 0; ptyp < 8; ptyp++)
  193.     for (po = 0; po < 64; po++)
  194.       for (p0 = 0; p0 < 64; p0++)
  195.     {
  196.       nextpos[ptyp][po][p0] = (unsigned char) po;
  197.       nextdir[ptyp][po][p0] = (unsigned char) po;
  198.     }
  199.   for (ptyp = 1; ptyp < 8; ptyp++)
  200.     for (po = 21; po < 99; po++)
  201.       if (nunmap[po] >= 0)
  202.     {
  203.       ppos = nextpos[ptyp][nunmap[po]];
  204.       pdir = nextdir[ptyp][nunmap[po]];
  205.       /* dest is a function of direction and steps */
  206.       for (d = 0; d < 8; d++)
  207.         {
  208.           dest[d][0] = nunmap[po];
  209.           delta = direc[ptyp][d];
  210.           if (delta != 0)
  211.         {
  212.           p0 = po;
  213.           for (s = 0; s < max_steps[ptyp]; s++)
  214.             {
  215.               p0 = p0 + delta;
  216.  
  217.               /*
  218.                * break if (off board) or (pawns only move two
  219.                * steps from home square)
  220.                */
  221.               if ((nunmap[p0] < 0) || (((ptyp == pawn) || (ptyp == bpawn))
  222.                            && ((s > 0) && ((d > 0) || (Stboard[nunmap[po]] != pawn)))))
  223.             break;
  224.               else
  225.             dest[d][s] = nunmap[p0];
  226.             }
  227.         }
  228.           else
  229.         s = 0;
  230.  
  231.           /*
  232.            * sort dest in number of steps order currently no sort
  233.            * is done due to compability with the move generation
  234.            * order in old gnu chess
  235.            */
  236.           steps[d] = s;
  237.           for (di = d; s > 0 && di > 0; di--)
  238.         if (steps[sorted[di - 1]] == 0)    /* should be: < s */
  239.           sorted[di] = sorted[di - 1];
  240.         else
  241.           break;
  242.           sorted[di] = d;
  243.         }
  244.  
  245.       /*
  246.        * update nextpos/nextdir, pawns have two threads (capture
  247.        * and no capture)
  248.        */
  249.       p0 = nunmap[po];
  250.       if (ptyp == pawn || ptyp == bpawn)
  251.         {
  252.           for (s = 0; s < steps[0]; s++)
  253.         {
  254.           ppos[p0] = (unsigned char) dest[0][s];
  255.           p0 = dest[0][s];
  256.         }
  257.           p0 = nunmap[po];
  258.           for (d = 1; d < 3; d++)
  259.         {
  260.           pdir[p0] = (unsigned char) dest[d][0];
  261.           p0 = dest[d][0];
  262.         }
  263.         }
  264.       else
  265.         {
  266.           pdir[p0] = (unsigned char) dest[sorted[0]][0];
  267.           for (d = 0; d < 8; d++)
  268.         for (s = 0; s < steps[sorted[d]]; s++)
  269.           {
  270.             ppos[p0] = (unsigned char) dest[sorted[d]][s];
  271.             p0 = dest[sorted[d]][s];
  272.             if (d < 7)
  273.               pdir[p0] = (unsigned char) dest[sorted[d + 1]][0];
  274.  
  275.             /*
  276.              * else is already initialized
  277.              */
  278.           }
  279.         }
  280.     }
  281. }
  282.  
  283. void
  284. NewGame (void)
  285.  
  286. /*
  287.  * Reset the board and other variables to start a new game.
  288.  */
  289.  
  290. {
  291.   short l, c, p;
  292. #ifdef HASGETTIMEOFDAY
  293.   struct timeval tv;
  294. #endif
  295. #ifdef CLIENT
  296.   if(GameCnt >0)ListGame();
  297.   fflush(stdout);
  298. #endif
  299.   compptr = oppptr = 0;
  300.   stage = stage2 = -1;        /* the game is not yet started */
  301.   flag.illegal = flag.mate = flag.post = flag.quit = flag.reverse = flag.bothsides = flag.onemove = flag.force = false;
  302.   flag.material = flag.coords = flag.hash = flag.easy = flag.beep = flag.rcptr = true;
  303.   flag.stars = flag.shade = flag.back = flag.musttimeout = false;
  304. #ifdef CLIENT
  305.   flag.gamein = true;
  306. #else 
  307.   flag.gamein = false;
  308. #endif
  309. #if defined(MSDOS) && !defined(SEVENBIT)
  310.   flag.rv = false;
  311. #else
  312.   flag.rv = true;
  313. #endif /* MSDOS && !SEVENBIT */
  314.   mycnt1 = mycnt2 = 0;
  315.   GenCnt = NodeCnt = et0 = epsquare =  dither =  XCmore = 0;
  316.   WAwindow = WAWNDW;
  317.   WBwindow = WBWNDW;
  318.   BAwindow = BAWNDW;
  319.   BBwindow = BBWNDW;
  320.   xwndw = BXWNDW;
  321.   if (!MaxSearchDepth)
  322.     MaxSearchDepth = MAXDEPTH - 1;
  323.   contempt = 0;
  324.   GameCnt = 0;
  325.   Game50 = 1;
  326.   hint = 0x0C14;
  327.   ZeroRPT ();
  328.   Developed[white] = Developed[black] = false;
  329.   castld[white] = castld[black] = false;
  330.   PawnThreat[0] = CptrFlag[0] = false;
  331.   Pscore[0] = Tscore[0] = 12000;
  332.   opponent = white;
  333.   computer = black;
  334.   for (l = 0; l < TREE; l++)
  335.     Tree[l].f = Tree[l].t = 0;
  336.  gsrand ((unsigned int) 1);
  337.   if (!InitFlag)
  338.     {
  339.       for (c = white; c <= black; c++)
  340.     for (p = pawn; p <= king; p++)
  341.       for (l = 0; l < 64; l++)
  342.         {
  343.           hashcode[c][p][l].key = (((unsigned long) urand ()));
  344.           hashcode[c][p][l].key += (((unsigned long) urand ()) << 16);
  345.           hashcode[c][p][l].bd = (((unsigned long) urand ()));
  346.           hashcode[c][p][l].bd += (((unsigned long) urand ()) << 16);
  347. #ifdef LONG64
  348.           hashcode[c][p][l].key += (((unsigned long) urand ()) << 32);
  349.           hashcode[c][p][l].key += (((unsigned long) urand ()) << 48);
  350.           hashcode[c][p][l].bd += (((unsigned long) urand ()) << 32);
  351.           hashcode[c][p][l].bd += (((unsigned long) urand ()) << 48);
  352. #endif
  353.         }
  354.     }
  355.   for (l = 0; l < 64; l++)
  356.     {
  357.       board[l] = Stboard[l];
  358.       color[l] = Stcolor[l];
  359.       Mvboard[l] = 0;
  360.     }
  361.   ClrScreen ();
  362.   InitializeStats ();
  363. #ifdef HASGETTIMEOFDAY
  364.   gettimeofday(&tv, NULL);
  365.   time0 = tv.tv_sec*100+tv.tv_usec/10000;
  366. #else
  367.   time0 = time ((long *) 0);
  368. #endif
  369.   ElapsedTime (1);
  370.   flag.regularstart = true;
  371.   Book = BOOKFAIL;
  372.   if (!InitFlag)
  373.     {
  374.     char sx[256];
  375.     strcpy(sx,CP[169]);
  376.       if (TCflag) SetTimeControl ();
  377.       else if (MaxResponseTime == 0) SelectLevel (sx);
  378.       UpdateDisplay (0, 0, 1, 0);
  379.       GetOpenings ();
  380. #if ttblsz
  381.       Initialize_ttable();
  382. #endif
  383.       InitFlag = true;
  384.     }
  385. #if ttblsz
  386.   if(TTadd){ZeroTTable (); TTadd = 0;}
  387. #endif /* ttblsz */
  388.   hashbd = hashkey = 0;
  389.   return;
  390. }
  391.  
  392. void
  393. InitConst (char *lang)
  394. {
  395.   FILE *constfile;
  396.   char s[256];
  397.   char sl[5];
  398.   int len, entry;
  399.   char *p, *q;
  400.   constfile = fopen (LANGFILE, "r");
  401.   if (!constfile)
  402.     {
  403.       printf ("NO LANGFILE\n");
  404.       exit (1);
  405.     }
  406.   while (fgets (s, sizeof (s), constfile))
  407.     {
  408.       if (s[0] == '!')
  409.     continue;
  410.       len = strlen (s);
  411.       for (q = &s[len]; q > &s[8]; q--)
  412.     if (*q == '}')
  413.       break;
  414.       if (q == &s[8])
  415.     {
  416.       printf ("{ error in cinstfile\n");
  417.       exit (1);
  418.     }
  419.       *q = '\0';
  420.       if (s[3] != ':' || s[7] != ':' || s[8] != '{')
  421.     {
  422.       printf ("Langfile format error %s\n", s);
  423.       exit (1);
  424.     }
  425.       s[3] = s[7] = '\0';
  426.       if (lang == NULL)
  427.     {
  428.       lang = sl;
  429.       strcpy (sl, &s[4]);
  430.     }
  431.       if (strcmp (&s[4], lang))
  432.     continue;
  433.       entry = atoi (s);
  434.       if (entry < 0 || entry >= CPSIZE)
  435.     {
  436.       printf ("Langfile number error\n");
  437.       exit (1);
  438.     }
  439.       for (q = p = &s[9]; *p; p++)
  440.     {
  441.       if (*p != '\\')
  442.         {
  443.           *q++ = *p;
  444.         }
  445.       else if (*(p + 1) == 'n')
  446.         {
  447.           *q++ = '\n';
  448.           p++;
  449.         }
  450.     }
  451.       *q = '\0';
  452.       if (entry < 0 || entry > 255)
  453.     {
  454.       printf ("Langfile error %d\n", entry);
  455.       exit (0);
  456.     }
  457.       CP[entry] = (char *) malloc ((unsigned) strlen (&s[9]) + 1);
  458.       if (CP[entry] == NULL)
  459.     {
  460.       perror ("malloc");
  461.       exit (0);
  462.     }
  463.       strcpy (CP[entry], &s[9]);
  464.  
  465.     }
  466.   fclose (constfile);
  467. }
  468.  
  469. #ifdef ttblsz
  470. void
  471.  
  472. Initialize_ttable ()
  473. {
  474.   int doit = true;
  475.   if (rehash < 0) rehash = MAXrehash;
  476. while(doit && ttblsize > MINTTABLE){
  477.   ttable[0] = (struct hashentry *)malloc((unsigned)(sizeof(struct hashentry)*(ttblsize+rehash)));
  478.   ttable[1] = (struct hashentry *)malloc((unsigned)(sizeof(struct hashentry)*(ttblsize+rehash)));
  479.   if(ttable[0] == NULL || ttable[1] == NULL){
  480.   if(ttable[0] != NULL)free(ttable[0]);
  481.   ttblsize = ttblsize>>1;
  482.   } else doit = false;
  483. }
  484.   if(ttable[0] == NULL || ttable[1] == NULL){ perror("memory alloc");exit(1);}
  485. /*
  486.   printf("transposition table is %d\n",ttblsize);
  487. */
  488.   ttbllimit = ttblsize<<1 - ttblsize>>2;
  489. }
  490.  
  491. #endif /* ttblsz */
  492.